import { Component, OnInit, Input } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { TreeNode } from 'primeng/api';
import { DynamicDialogRef, DynamicDialogConfig } from 'primeng/api';

@Component({
    templateUrl: './tab-form-print.component.html',
    styleUrls: ['./tab-form-print.component.scss']
})
export class TabFormPrintComponent implements OnInit {
    loading = true;
    formId: number = 0;
    selectedForm: any;
    data: any;
    forms: TreeNode[] = [];
    recId: number = 0;
    actId: number = 0;
    actDefId: number = 0;
    bizId: number =0;
    systemUrl: string
    constructor(
        private http: HttpClient,
        private ref: DynamicDialogRef,
        private config: DynamicDialogConfig) { }

    ngOnInit() {
        this.data = this.config.data || {};
        this.recId = this.data.recId;
        this.actId = this.data.actId;
        this.bizId = this.data.bizId;
        this.actDefId = this.data.actDefId;
        this.http.get('/' + this.systemUrl + '/api/tab/form/listprintformtree?recId=' + this.recId + '&actDefId=' + this.actDefId + '&bizId=' + this.bizId).subscribe(data => {
            this.forms = data['data'];
            this.loading = false;
            if (this.forms.length > 0) {
                let temp = this.forms[0];
                while (temp.icon.indexOf('folder') >= 0) {
                    temp = temp.children[0];
                }
                this.selectedForm = temp;
                this.nodeSelect(null);
            }
        });
    }

    /** 节点选择 */
    nodeSelect($event) {
        this.formId = this.selectedForm['id'];
        this.http.post('/form/api/value/printview', { 'bizId': this.bizId, 'relKey': this.recId, 'formVersion': 0, 'formId': this.formId }).subscribe(data => {
            var pdfData = atob(data['data']['pdfContent']); //$.base64.
            var uint8Array = new Uint8Array(pdfData.length);
            for (var i = 0; i < pdfData.length; i++) {
                uint8Array[i] = pdfData.charCodeAt(i);
            }
            window['printPdfContent'] = { 'pdfName': this.selectedForm.label + '.pdf', 'pdfContent': uint8Array };
            $("#pdfConcent").attr("src", "/assets/other/pdfjs/viewer.html").ready();
            $("#pdfConcent").fadeIn(500);
        });
    }

}